/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package co.edu.senaclem.persistencia;

import co.edu.senaclem.modelo.ClienteExterno;
import co.edu.senaclem.modelo.Personal;
import static co.edu.senaclem.persistencia.Persistencia.areadb;
import static co.edu.senaclem.persistencia.Persistencia.con;
import static co.edu.senaclem.persistencia.Persistencia.conectar;
import static co.edu.senaclem.persistencia.Persistencia.desconectar;
import java.sql.ResultSet;
import javax.ejb.Stateless;

/**
 *
 * @author anfeles
 */
@Stateless
public class ClienteExternoDAO implements IClienteExternoDAO{

    public static ResultSet resSet=null;
    
    @Override
    public ClienteExterno consultarPorId(Long documento) throws Exception {
        try {
            ClienteExterno miCliente = null;
            if (conectar()) {
                String consulta = "Select * From clienteexterno where documentocliente='" + documento + "'";
                areadb = con.createStatement();
                resSet = areadb.executeQuery(consulta);

                if (resSet.next()) {
                    miCliente = new ClienteExterno();
                    IPersonalDAO personalDAO=new PersonalDAO();                    
                    Personal miPersonal=personalDAO.consultarPorId(resSet.getLong("documentocliente"));                                        
                    miCliente.setDocumento(miPersonal);                  
                }

                resSet.close();
                desconectar();
            }

            return miCliente;
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void insertar(ClienteExterno clienteExterno) throws Exception {
        try {
            if (conectar()) {
                String consulta = "insert into clienteexterno values('" + clienteExterno.getDocumento().getDocumento()+ "')";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }
    
}
